package com.example.tms.controller;


import com.example.tms.bean.*;
import com.example.tms.mapper.OilPriceMapper;
import com.example.tms.mapper.RoadMapper;
import com.example.tms.tools.GlobalParam;
//import com.sun.tools.javac.jvm.ClassWriter;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.tms.annotation.*;
@RestController
public class OilPriceController {
    private final OilPriceMapper oilPriceMapper;

    @Autowired
    public OilPriceController(OilPriceMapper oilPriceMapper){
        this.oilPriceMapper = oilPriceMapper;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    //查询报账信息(报账id)
    @RequiresPermissions("161")
    @RequestMapping(value = "oilprice/{ydjidid}",method = RequestMethod.GET)
    public OilPrice getOilPriceInfo(@PathVariable String ydjid){
        return oilPriceMapper.findByYdjidAndSfcz(ydjid,true);
    }

    //查询报账信息列表
    @RequiresPermissions("161")
    @RequestMapping(value = "oilprice",method = RequestMethod.GET)
    public ReturnInfo getOilPriceInfoAll(@RequestParam(required = false, defaultValue = "") String keyname,
                                         @RequestParam(required = false, defaultValue = "") String keyvalue) {
        List<OilPrice> result = oilPriceMapper.findAll((Root<OilPrice> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            switch (keyname) {
                case "ydjqsrq":
                    list.add(criteriaBuilder.like(root.get("ydjqsrq").as(String.class), "%" + keyvalue + "%"));
                    break;

                default:
                    break;
            }
            list.add(criteriaBuilder.equal(root.get("sfcz").as(Boolean.class),true));
            Predicate[] p = new Predicate[list.size()];
            criteriaQuery.where(criteriaBuilder.and(list.toArray(p)));
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("ydjqsrq")));
            return criteriaQuery.getRestriction();
        });
        return ReturnInfo.info2(result);
    }

    //新建报账信息
    @MonitorRequest
    @RequiresPermissions("162")
    @RequestMapping(value = "oilprice",method = RequestMethod.POST)
    public List<Map<String,Object>> saveOilPriceInfo(@RequestBody OilPrice oilPrice){
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object>  map = new HashMap<>();
        if(oilPrice.getYdjqsrq().equals("")){
            map.put("ydjqsrq", "0");
        }
        if(oilPrice.getXydj().equals("")||!checkMoney(oilPrice.getXydj())){
            map.put("xydj", "0");
        }
        if(oilPrice.getSzrq().equals("")){
            map.put("szrq", "0");
        }
        if(oilPrice.getYdjszr().equals("")){
            map.put("ydjszr", "0");
        }
        list.add(map);
        if(map.isEmpty()){
            oilPrice.setLasttime(GlobalParam.getTime());
            oilPrice.setSfcz(true);
            oilPriceMapper.save(oilPrice);
            logger.info("更新油单价"+oilPrice.toString());

        }
        return list;
    }

    //更新报账信息
    @MonitorRequest
    @RequiresPermissions("163")
    @RequestMapping(value = "oilprice",method = RequestMethod.PUT)
    public List<Map<String,Object>> updateOilPriceInfo(@RequestBody OilPrice oilPrice){
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object>  map = new HashMap<>();
        if(oilPrice.getYdjid()==null||oilPriceMapper.findByYdjidAndSfcz(oilPrice.getYdjid(),true)==null){
            map.put("ydjid", "0");
        }
        if(oilPrice.getYdjqsrq().equals("")){
            map.put("ydjqsrq", "0");
        }
        if(oilPrice.getXydj().equals("")||!checkMoney(oilPrice.getXydj())){
            map.put("xydj", "0");
        }
        if(oilPrice.getSzrq().equals("")){
            map.put("szrq", "0");
        }
        if(oilPrice.getYdjszr().equals("")){
            map.put("ydjszr", "0");
        }
        list.add(map);
        if(map.isEmpty()){
            oilPrice.setLasttime(GlobalParam.getTime());
            oilPrice.setSfcz(true);
            oilPriceMapper.save(oilPrice);
            logger.info("更新油单价"+oilPrice.toString());

        }
        return list;
    }

    //删除报账信息（假删除 报账id）
    @MonitorRequest
    @RequiresPermissions("164")
    @RequestMapping(value = "oilprice/{ydjid}",method = RequestMethod.DELETE)
    public ReturnInfo deleteOilOriceinfo(@PathVariable String ydjid){
        String b;
        if(ydjid==null||oilPriceMapper.findByYdjidAndSfcz(ydjid,true)==null){
            b = "该油单价信息不存在！";
        }else{
            oilPriceMapper.DeleteByYdjid(ydjid);
            b = "删除成功！";
            logger.info("删除油单价"+ydjid);
        }
        return ReturnInfo.info1(b);
    }

    //检查金额
    private Boolean checkMoney(String  xydj){
        Boolean b = false;
        if(xydj.matches("^(-)?([1-9]\\d{0,9}|0)(.\\d{1,4})?$")){
            b = true;
        }
        return b;
    }
}
